# -*- coding: utf-8 -*-
# @Time    : 2024/7/9 9:47
# @Author  : wangwei
# @File    : excel_ddt.py
import os

import pytest
import datetime
from common.Excel import Writer,Reader
from common.Logger import logger
from common.webkeyword import WebKeyword
import datetime

class DDT:
    webkeyword=WebKeyword()

    def __init__(self):
        self.end_time=""
        self.start_time=""
        self.reader=Reader()
        self.writer = Writer()
        self.testsuite=[]
        self.testcase=[]
        self.suite_idx=0
        self.case_idx=0
        self.feature_idx=0
        self.story_idx = 0
        self.feature=None
        self.story=None
        self.write_row=0
        self.run_idx=0
        self.sheets=None

    def run_web_cases(self,case_path='电商项目用例.xlsx'):
        # 打开一个excel
        #记录开始时间
        self.start_time = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        print(self.start_time)

        self.reader.open_excel(f"\\lib\\cases\\{case_path}")
        self.writer.copy_open(f"\\lib\\cases\\{case_path}", f"\\lib\\cases\\result-{case_path}")
        # 获取所有sheet
        self.sheets = self.reader.get_sheets()
        print(self.sheets)
        self.writer.set_sheet(self.sheets[0])
        ddt.writer.write(1,3, self.start_time, 2)
        for sheet in self.sheets:
            self.write_row = 0
            # 设置读取的sheet页面
            self.feature_idx+=1
            self.feature=sheet
            self.story_idx=0
            self.reader.set_sheet(sheet)
            # 读取当前sheet的所有行
            self.writer.set_sheet(sheet)
            lines = self.reader.readline()
            self.write_row+=1
            for i in range(1,len(lines)):
                line=lines[i]
                if line[0]:

                    if self.testcase:
                        self.testsuite.append(self.testcase)
                        #输出结果
                        self.__run_test()
                    self.testcase=[]
                    self.testsuite=[]
                    self.story_idx += 1
                    self.story = line[0]
                    self.write_row += 1
                elif line[1]:
                    if self.testcase:
                        self.testsuite.append(self.testcase)
                    self.testcase=[]
                    self.testcase.append(line)
                else:
                    self.testcase.append(line)
            if self.testcase:
                self.testsuite.append(self.testcase)
                # 输出结果
                self.__run_test()
            self.testsuite=[]
            self.testcase=[]

    def __run_test(self):
        try:
            os.rename("./testcases/test_web_0.py",f"./testcases/test_web_{self.suite_idx+1}.py")
            pytest.main(["-vs",f"./testcases/test_web_{self.suite_idx+1}.py","--alluredir","./result"])
            self.suite_idx+=1
        except Exception as e:
            print(e.__str__())
        finally:
            os.rename(f"./testcases/test_web_{self.suite_idx}.py",f"./testcases/test_web_0.py")
            # 记录结束时间
            self.end_time = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            print(self.end_time)
            self.writer.set_sheet(self.sheets[0])
            ddt.writer.write(1, 4, self.end_time, 2)
            ddt.writer.save_close()





        #


        # #
        # # 获取所有sheet
        # sheets = self.writer.get_sheets()
        # self.writer.set_sheet(sheets[0])
        # # 在原格式上写入内容
        # self.writer.write(1, 1, 'Will1')
        # # 更改颜色写入
        # self.writer.write(1, 2, 'hello1', color=2)
        # self.writer.save_close()


ddt=DDT()

if __name__ == '__main__':
    ddt.run_web_cases()